﻿using Windows.Storage;

namespace MarginCalc.Settings
{
    public static class DecimalSettingsStore
    {
        public static void SaveDecimal(string key, decimal value)
        {
            if (ApplicationData.Current.LocalSettings.Values.ContainsKey(key))
            {
                if (ApplicationData.Current.LocalSettings.Values[key].ToString() != null)
                {
                    // do update
                    ApplicationData.Current.LocalSettings.Values[key] = value.ToString();
                }
            }
            else
            {
                // do create key and save value, first time only.
                ApplicationData.Current.LocalSettings.CreateContainer(key, ApplicationDataCreateDisposition.Always);

                if (ApplicationData.Current.LocalSettings.Values[key] == null)
                {
                    ApplicationData.Current.LocalSettings.Values[key] = value.ToString();
                }
            }
        }

        public static decimal? GetDecimalValue(string key)
        {
            decimal result;
            return decimal.TryParse(ApplicationData.Current.LocalSettings.Values[key] as string, out result) ? (decimal?) result : null;
        }
    }
}